package com.tencent.miniproject.ui;

import android.content.Intent;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.tencent.miniproject.R;
import com.tencent.miniproject.base.BaseActivity;
import com.tencent.miniproject.been.LabelAndTimes;
import com.tencent.miniproject.constants.Constant;
import com.tencent.miniproject.presenter.SearchPresenter;
import com.tencent.miniproject.presenter.i.ISearchPresenter;
import com.tencent.miniproject.ui.i.ISearchActivity;
import com.tencent.miniproject.utils.ProgressDialogUtil;
import com.tencent.miniproject.view.EditTextWithDel;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;


public class SearchActivity extends BaseActivity implements ISearchActivity {
    @BindView(R.id.etw_search)
    EditTextWithDel mEtwSearch;
    @BindView(R.id.rl_command1)
    RelativeLayout mRlCommand1;
    @BindView(R.id.rl_command2)
    RelativeLayout mRlCommand2;
    @BindView(R.id.rl_command3)
    RelativeLayout mRlCommand3;
    @BindView(R.id.rl_command4)
    RelativeLayout mRlCommand4;
    @BindView(R.id.rl_command5)
    RelativeLayout mRlCommand5;
    @BindView(R.id.tv_command1)
    TextView mTvCommand1;
    @BindView(R.id.tv_command2)
    TextView mTvCommand2;
    @BindView(R.id.tv_command3)
    TextView mTvCommand3;
    @BindView(R.id.tv_command4)
    TextView mTvCommand4;
    @BindView(R.id.tv_command5)
    TextView mTvCommand5;
    @BindView(R.id.tv_sum1)
    TextView mTvSum1;
    @BindView(R.id.tv_sum2)
    TextView mTvSum2;
    @BindView(R.id.tv_sum3)
    TextView mTvSum3;
    @BindView(R.id.tv_sum4)
    TextView mTvSum4;
    @BindView(R.id.tv_sum5)
    TextView mTvSum5;

    private ISearchPresenter mPresenter;

    @Override
    protected int setLayoutResID() {
        return R.layout.activity_search;
    }

    @Override
    protected void initData() {
        mPresenter = new SearchPresenter(this);
        ProgressDialogUtil.showProgressDialog(this,"正在获取热门搜索");
        mPresenter.getHotTag();
    }

    @Override
    protected void initView() {
    }

    @Override
    protected void initListener() {
        mEtwSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    String searchContent = mEtwSearch.getText().toString();
                    startActivity(new Intent(SearchActivity.this, MapActivity.class).putExtra("type", Constant.MAP_KEYWORD)
                            .putExtra("keyword", searchContent));
                    return true;
                }
                return false;
            }
        });
    }

    @OnClick({R.id.rl_command1, R.id.rl_command2, R.id.rl_command3, R.id.rl_command4, R.id.rl_command5})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_command1:
//                showToast(mTvCommand1.getText().toString());
                startActivity(new Intent(this, MapActivity.class).putExtra("type", Constant.MAP_KEYWORD)
                        .putExtra("keyword", mTvCommand1.getText().toString()));
                break;
            case R.id.rl_command2:
//                showToast(mTvCommand2.getText().toString());
                startActivity(new Intent(this, MapActivity.class).putExtra("type", Constant.MAP_KEYWORD)
                        .putExtra("keyword", mTvCommand2.getText().toString()));
                break;
            case R.id.rl_command3:
//                showToast(mTvCommand3.getText().toString());
                startActivity(new Intent(this, MapActivity.class).putExtra("type", Constant.MAP_KEYWORD)
                        .putExtra("keyword", mTvCommand3.getText().toString()));
                break;
            case R.id.rl_command4:
//                showToast(mTvCommand4.getText().toString());
                startActivity(new Intent(this, MapActivity.class).putExtra("type", Constant.MAP_KEYWORD)
                        .putExtra("keyword", mTvCommand4.getText().toString()));
                break;
            case R.id.rl_command5:
//                showToast(mTvCommand5.getText().toString());
                startActivity(new Intent(this, MapActivity.class).putExtra("type", Constant.MAP_KEYWORD)
                        .putExtra("keyword", mTvCommand5.getText().toString()));
                break;
        }
    }

    @Override
    public void getHotTagSuccess(List<LabelAndTimes> messages) {
        ProgressDialogUtil.dismiss();
        if (messages == null){
            return;
        }
        LabelAndTimes hotTagMessage0 = messages.get(0);
        mTvCommand1.setText(hotTagMessage0.getLabel());
        mTvSum1.setText(hotTagMessage0.getTimes());
        LabelAndTimes hotTagMessage1 = messages.get(1);
        mTvCommand2.setText(hotTagMessage1.getLabel());
        mTvSum2.setText(hotTagMessage1.getTimes());
        LabelAndTimes hotTagMessage2 = messages.get(2);
        mTvCommand3.setText(hotTagMessage2.getLabel());
        mTvSum3.setText(hotTagMessage2.getTimes());
        LabelAndTimes hotTagMessage3 = messages.get(3);
        mTvCommand4.setText(hotTagMessage3.getLabel());
        mTvSum4.setText(hotTagMessage3.getTimes());
        LabelAndTimes hotTagMessage4 = messages.get(4);
        mTvCommand5.setText(hotTagMessage4.getLabel());
        mTvSum5.setText(hotTagMessage4.getTimes());
    }

    @Override
    public void getHotTagError(String msg) {
        ProgressDialogUtil.dismiss();
        showToast(msg);
    }

}
